package com.zjsru.oneDay202204;

import java.util.Arrays;

/**
 * @Author: likew
 * @Date: 2022/4/22
 * 旋转函数
 *
 * 输入: nums = [4,3,2,6] (顺时针旋转)
 * 输出: 26
 * 解释:
 * F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
 * F(1) = (0 * 6) +  = 0 + 4 + 6 + 6 = 16
 * F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23
 * F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26
 * 所以 F(0), F(1), F(2), F(3) 中的最大值是 F(3) = 26 。
 *
 */
public class maxRotateFunction {
    /**未通过全部测试用例*/
//    public int maxRotateFunction(int[] nums) {
//        int res = 0,ans = 0;
//        int len = nums.length;
//        for (int times = len -1; times > 0; times--) {
//            for (int i = 0, j=0; i < len; i++) {
//                ans += nums[i]*j;
//                j++;
//            }
//            res = Math.max(ans,res);
//            ans = 0;
//        对数组进行旋转
//        nums = swap(nums,len,times);
//        }
//        return res;
//    }
//
//    private int[] swap(int[] nums, int len,int times) {
//        while(times > 0){
//            int j = 0;
//            int temp = 0;
//            for (int i = 0; i < len; i++) {
//                temp = nums[len - 1];
//                nums[len - 1] = nums[j];
//                nums[j] = temp;
//                j++;
//            }
//            times--;
//        }
//        return nums;
//    }

    /**
     * 数学方法
     * */
    public int maxRotateFunction(int[] nums) {
        int f = 0, numSum = Arrays.stream(nums).sum();
        int len = nums.length;
        int res = 0;
        for (int i = 0; i < len; i++) {
            f += nums[i] * i;
        }
        res = f;
        for (int i = len - 1; i > 0; i--) {
            f += numSum - len * nums[i];
            res = Math.max(f,res);
        }
        return res;
    }
    public static void main(String[] args) {
        maxRotateFunction maxRotateFunction = new maxRotateFunction();
        int[] nums = new int[]{4,3,2,6};
        System.out.println(maxRotateFunction.maxRotateFunction(nums));
    }
}
